package com.yz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yz.dto.SetmealDto;
import com.yz.entity.Category;
import com.yz.entity.Setmeal;
import com.yz.entity.SetmealDish;
import com.yz.mapper.SetmealMapper;
import com.yz.service.CategoryService;
import com.yz.service.SetMealDishService;
import com.yz.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private SetMealDishService setMealDishService;
    /**
     * 保存新添加的套餐数据
     * @param setmealDto
     * @return
     */
    @Transactional
    public Boolean saveAndSetmealDish(SetmealDto setmealDto) {
        this.save(setmealDto);

        // 获取SetMealDish数组
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        // 添加setmealId  设置sort排序
        Long setMealId = setmealDto.getId();
        List<SetmealDish> collect = setmealDishes.stream().map(item -> {
            item.setSetmealId(setMealId);
            return item;
        }).collect(Collectors.toList());
        setMealDishService.saveBatch(collect);
        return true;
    }

    /**
     * 获取套餐分页数据
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public Page<SetmealDto> pageAndCategoryName(int page, int pageSize) {
        // 获取套餐数据列表
        Page<Setmeal> setmealPage = new Page<>(page,pageSize);
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(Setmeal::getUpdateTime);
        this.page(setmealPage,wrapper);

        // 根据菜品分类id 获取菜品分类名称 并放入SetMealDto
        Page<SetmealDto> setmealDtoPage = new Page<>();
        BeanUtils.copyProperties(setmealPage,setmealDtoPage,"records");
        List<SetmealDto> collect = setmealPage.getRecords().stream().map(item -> {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(item, setmealDto);

            Category category = categoryService.getById(item.getCategoryId());
            if (category == null)
                setmealDto.setCategoryName("无套餐分类");
            else
                setmealDto.setCategoryName(category.getName());
            return setmealDto;
        }).collect(Collectors.toList());
        setmealDtoPage.setRecords(collect);
        return setmealDtoPage;
    }

    /**
     * 删除套餐 单个或者多个
     * @param ids
     * @return
     */
    @Transactional
    public boolean deleteAndSetmeanDish(Long[] ids) {
        LambdaQueryWrapper<SetmealDish> wrapper = null;
        for (Long id:ids){
            wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(SetmealDish::getSetmealId,id);
            setMealDishService.remove(wrapper);
        }
        this.removeByIds(Arrays.asList(ids));
        return true;
    }
}
